#include "vfs.h"

#include "file.h"


namespace evm { namespace vfs {

string read(const string& path) {
// Read 1KB max bytes each time, until total 1MB max bytes.
    static constexpr int MAX_CHUNKS = 1024;
    static constexpr int CHUNK_SIZE = 1024;

    string bytes;
    size_t size = 0;
    File f(path);
    for (int i = 1; i < MAX_CHUNKS; ++i) {
        size += CHUNK_SIZE;
        bytes.resize(size);
        size_t count = CHUNK_SIZE;
        auto ret = f.read(&bytes[size - CHUNK_SIZE], count);
        bytes.resize(size - CHUNK_SIZE + count);
        if (!ret)
            break; // has reached eof
    }

    return std::move(bytes);
}

void write(const string& path, const string& contents) {
    File::write_all(path, contents);
}

}}
